import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'lib_music_platform_interface.dart';
import 'listener/OnFftDataListener.dart';
import 'listener/OnMusicCallback.dart';

/// An implementation of [LibMusicPlatform] that uses method channels.
class MethodChannelLibMusic extends LibMusicPlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('lib_music');

  //音乐播放回调接口
  OnMusicCallback? onMusicCallback ;

  OnFftDataListener? onFftDataListener;


  @override
  void setOnMusicCallback(OnMusicCallback callback){
    onMusicCallback = callback;
  }
  @override
  void setOnFftDataListener(OnFftDataListener listener){
    onFftDataListener = listener;
  }

  MethodChannelLibMusic() {
    methodChannel.setMethodCallHandler((call) async {
      switch (call.method) {
        case "onStartPlay":
          onMusicCallback?.onStartPlay();
          break;
        case "onPausePlay":
          onMusicCallback?.onPausePlay();
          break;
        case "onPlayComplete":
          onMusicCallback?.onPlayComplete();
          break;
        case "onProgress":
          onMusicCallback?.onProgress(call.arguments);
          break;
        case "onFftDataCapture":
          List bytes = call.arguments;
          List<int> l = [];
          for (var b in bytes) {
            l.add(b);
          }
          onFftDataListener?.onFftDataCapture(l);
          break;
        case "onWaveFormDataCapture":
          break;
      }
    });
  }

  @override
  Future<String?> getPlatformVersion() async {
    final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }

  @override
  Future<T?> invokeMethod<T>(String method, [dynamic arguments]) {
    return methodChannel.invokeMethod<T>(method, arguments);
  }
}
